<template>
    <div>
        <div class="carnumber">
            <div class="carnumber-mark">新能源</div>
            <div class="warp" >
            <div  v-for="(item, index) in fill"  @click="fopcusHandler(index)" :key="index"  :class="[index === length-1 ? 'lasts':'',(index === length-1 || index === length-2) ? 'no-border':'', 'cell', index == current ? 'active':'']">
                <div class="val">{{item}}</div>
                <div class="border"></div>
            </div>
            </div>
            <div class="btn-box">
              <!-- <button class="btns" @click="editNumber">减免</button> -->
              <!-- <button class="btns stick" @click="handlePaste">粘贴</button> -->
              <button class="btns" @click="editNumber">确定</button>
            </div>
            <!-- 键盘 -->
            <div class="keyboard" v-if="focus">
            <!-- <div class="status-bar">
                <div class="close"  @click="keyHideHandler">关闭</div>
            </div> -->
            <div class="key-box">
                <div class="row" v-for="(item, index) in keys" :key="index">
                <div v-for="(val, j) in item" :key="j"  :class="[type== 'provinces' ? 'key-txt': 'key', index == item.length - 1 ? 'last':'',(val == '-' || val == '=') ? 'is-delete':'' ]" @click="keyTapHandler(val)" >  
                    <div class="delete" v-if="(val == '-' || val == '=')"><img  class="de" src="@/assets/icon-numfail.png"/></div>
                    <div v-else class="txt">{{val}}</div>
                </div>
                </div>
            </div>
            </div>
  </div>
  </div>
</template>

<script>
  export default {
      props: {
          carnumner: {
            type: String
            },
            
      },
      data() {
          return {
              areas:[
                [ "1", "2", "3", "4", "5", "6", "7", "8", "9", "0" ],
                [ "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P" ],
                [ "A", "S", "D", "F", "G", "H", "J", "K", "L" ],
                [ "Z", "X", "C", "V", "B", "N", "M", "-" ],
                ],
                provinces:[
                [ "京", "津", "沪", "渝", "川", "新", "藏", "宁", "桂", "贵" ],
                [ "云", "黑", "吉", "辽", "晋", "冀", "青", "鲁", "豫", "苏" ],
                [ "皖", "浙", "闽", "赣", "湘", "鄂", "粤", "琼", "甘", "陕" ],
                [ "蒙", "港", "澳", "台", "使", "领", "警", "学", "=" ],
                ],
                type: 'provinces',
                length:8,
                fill:new Array(8).fill(''),
                current: 0,
                focus:true,
                keys: [],
                deleteKeys: ['-', '='],
          }
      },
    
      mounted (){
        this.fill[0] = '川'
        this.fill[1] = 'A',
        // this.fill[2] = 'D'
        this.current = 2
        this.changeType()
      },
      watch: {
        current: function(current) {
        var curType =current === 0 ? 'provinces' : 'areas'
        // console.log('watch current',current,curType)
        this.type = curType
        this.changeType()
        },
        carnumber: function(carnumber) {
        // console.log('当前carnumber',carnumber)
        },
    },
    methods: {
        setCar(val) {
            if(val){
                var data = []
                for(let i=0; i< val.length; i++){
                var is=  val.charAt(i)
                data.push(is)
                }
                this.setCar = data
                this.current = 7
            }
    },
    handlePaste() {
        //获得粘贴的文字
        let that = this
        navigator.clipboard
        .readText()
        .then((v) => {
          var creg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1}$/
          if(!creg.test(v)){
            that.$message.error('车牌格式不正确');
            return
          }
          that.fill = v
          console.log("获取剪贴板成功：", v, that.fill);
        })
        .catch((v) => {
          // console.log("获取剪贴板失败: ", v);
        });
        // var content = window.getSelection();
        // console.log('获得粘贴的文字', content)
        return
        content  = '川ADF8888'
        var plate = content.split('')
                // console.log(content )
        // var text = VueClipboard;
        // console.log('获得粘贴的文字', plate)
        this.fill = plate
       
    },
    keyHideHandler() {
      this.focus = false
    },
    changeType () {
      let data = this[this.type]
      // console.log('当前car类型',this.type,data, this.fill)
      this.keys = data
    },
    editNumber() {
      var detail =''
      let fills = this.fill.map(item => {
       return detail += item
      })
      // console.log(detail)
      this.$emit('confirms', detail)
    },
    fopcusHandler(e) {
        // console.log('e',e)
      var index = e
      if (!index) { index = 0}
      this.focus = true
      this.current = index
      // console.log('this.current',this.current,e);
    },
    keyTapHandler(e) {
        // console.log('keyTapHandlere', e)
      var key = e
      if (this.deleteKeys.includes(key)) {
        this.keyDeleteHandler();
        return;
      }
      this.keyInputHandler(key);
    },
    // input
    keyInputHandler(key) {
      //  console.log('keyno',this.current)
    //   var temp= this.fill[this.current]
      this.fill[this.current] = key
      if (this.current >= this.length ) {
        return;
      }
      //  console.log('key', key, this.fill, this.current)
      var cur =  this.current += 1;
      this.current = cur
      
    },
    keyDeleteHandler() {
      // console.log('temp', this.keys)
      this.$set(this.fill, this.current, '')
      // this.fill[this.current] =  ''
      var cur =this.current ===0? 0 :  this.current -= 1;
      this.current = cur
    },
    }
  }
</script>
<style scoped>
  /* 键盘 */
.keyboard {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  background: #f5f5f5;
}

.status-bar {
  height: 40px;
  background: #8bb9f3;
  color: #fff;
  display: flex;
  justify-content: flex-end;
}

.close {
  font-size: 15px;
  height: 100%;
  padding: 0 10px;
  display: flex;
  align-items: center;
}
.key-box {
  padding: 12px 7px 15px 7px;
}
.row {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  margin-bottom: 8px;
}
.key {
  min-width: 9%;
  min-height: 53px;
  background: #FFFFFF;
  border: 1px solid #ececec;
  box-sizing: border-box;
  color: #444444;
  font-size: 18px;
  font-weight: bold;
  display: flex;
  align-items: center;
  justify-content: center;
  /* padding: 0 6px; */
  margin-right: 2px;
}
.key-txt {
  min-width: 9%;
  min-height: 53px;
  background: #FFFFFF;
  border: 1px solid #ececec;
  box-sizing: border-box;
  color: #444444;
  font-size: 12px;
  font-weight: bold;
  display: flex;
  align-items: center;
  justify-content: center;
  /* padding: 0 6px; */
  margin-right: 2px;  
}
.btn-box {
  display: flex;
  align-items: center;
  justify-content: flex-end;

}
.btns {
  width: 80px;
  height: 30px;
  color: #3a8ee6;
  margin-top: 10px;
  margin-left: 10px;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 10px;
  border: 1px solid #57caed;
  background-image: linear-gradient(#fff, #fff);
}
.stick {
  color: #b1b1b1;
  border: 1px solid #b1b1b1;
  background-image: #fff;
}
.delete {
  width: 15px;
  height: 15px;
  /* background: chocolate; */
}
.delete image {
  width: 100%;
  height: 100%;
}

.is-delete {
  width: 30px;
}
.last {
  margin-right: 0;
}

/* 伪输入框 */
.carnumber {
  position: relative;
  width: 100%;
  background: #fff;
}
.warp {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: space-between;
  border: 1px solid #dedada;
  box-sizing: border-box;
  border-radius: 8px;
  box-shadow: 0px 6px 6px 0px #efefef;
  padding: 14px 0;
}

.carnumber-mark {
  font-size: 12px;
  color: #646262;
  margin-bottom: 12px;
  display: flex;
  flex-direction: row-reverse;
  padding-right: 6px;
}
.cell {
  box-sizing: border-box;
  padding: 2px 0 ;
  display: flex;
  flex: 1;
  color: #666666;
  font-weight: bold;
    font-size: 17px;
  border-right: 1px solid #CCCCCC;
  text-align: center;
  border-bottom: 1px solid transparent;
  flex-direction: column;
  position: relative;
  height: 30px;
  justify-content: flex-start;
    align-items: center;
}
.val {
  display: flex;
  flex: 1;
  align-items: center;
  justify-content: center;
}

.border {
  height: 2px;
  flex-shrink: 0;
  flex-grow: 0;
  background-color: transparent;
  width: 80%;
}
.active .border {
  background: #FE8525;
}
.active .border::after{
  content: "";
    width: 100%;
    border: 2px solid #47dc47;
    border-radius: 8px;
    margin: -12px 0;
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
}
.no-border {
  /* border-right: none; */
}
/* .lasts::after {
  content: "";
  width: 100%;
  border: 2px solid #47dc47;
  border-radius: 8px;
  margin: -12px 0;
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  box-sizing: border-box;
} */
.de {
  width: 100%;
  height: 100%;
}
</style>
